//
//  ReadDetailHelper.m
//  News
//
//  Created by lanou3g on 15/11/12.
//  Copyright © 2015年 songhaowen. All rights reserved.
//

#import "ReadDetailHelper.h"
#import "ReadModel.h"
#import <UIKit/UIKit.h>

@interface ReadDetailHelper ()

@property (nonatomic, copy) NSString *body;
@property (nonatomic, copy) NSString *html;
@property (nonatomic, copy) NSString *sourcestring;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *s;

@end

@implementation ReadDetailHelper

SingletonM(ReadDetailHelper)

- (void)getDataFromWebWithString:(ReadModel *)model
{
    
        self.body = nil;
        NSString *string = [kDetailUrl_read stringByAppendingString:model.docid];
        NSString *strin = [string stringByAppendingString:@"/full.html"];
    
        NSDictionary *dict = [self webDataWithStr:strin];
        
        //解析数据
        NSDictionary *secondDic = [dict objectForKey:model.docid];
        
        self.html = [secondDic objectForKey:@"body"];
        
        NSString *ptime = [secondDic objectForKey:@"ptime"];
        NSString *source = [secondDic objectForKey:@"source"];
        NSString *str1 = @"来源:";
        NSString *strr = [str1 stringByAppendingString:source];
        self.sourcestring = [strr stringByAppendingString:ptime];
        self.title = [secondDic objectForKey:@"title"];
        
        
        NSArray *imgArray = [secondDic objectForKey:@"img"];
        
        for (NSDictionary *smalDic in imgArray) {
            NSString *daxiao = [smalDic objectForKey:@"pixel"];
            NSArray *w_h = [daxiao componentsSeparatedByString:@"*"];
            
            
            self.s = [NSString stringWithFormat:@"<img src=\"%@\" width=\"%f\" height=\"%f\"></p><p><font size=\"2px\"color=#A8A8A8>%@</font></p>", smalDic[@"src"], kScreenWidth - 20, [w_h[1] floatValue] / ([w_h[0] floatValue]/kScreenWidth),smalDic[@"alt"]];
            
            self.html = [self.html stringByReplacingOccurrencesOfString:smalDic[@"ref"] withString:self.s];
        }
    
        
        //完全布局
        self.body = [NSString stringWithFormat:@"<p><font size=\"5px\">%@</font></p><p><font size=\"2px\"color=#A9A9A9>%@\n</font><font size=\"2px\"color=#A9A9A9>%@</font></p> <div class  = \"text\" style = \"text-align:left; padding-left:2px;\">%@</div>", model.title,model.source,model.ptime,self.html];
    
        self.body = [self.body stringByReplacingOccurrencesOfString:@"<p>　　—————————————————————————————————————————</p>" withString:@"<p>-------------------------------------------------------------------</p>"];
        
        if (_block) {
            _block(self.body);
        }
    
}

- (NSDictionary *)webDataWithStr:(NSString *)str
{
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    //    NSLog(@"%@",data);
    
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    
    return dict;
}


@end
